# -*-encoding: utf-8 -*-

import httplib, urllib, urllib2, json, base64

class RequestUtil:
    def __init__(self, url, path, dataDict):
        self.url = url
        self.path = path
        self.dataDict = dataDict
        pass

    def postImpl(self):
        print '[URL]' + self.url
        print '[PATH]' + self.path
        dataJson = json.dumps(self.dataDict, ensure_ascii=False)
        print '[DATAJSON]' + dataJson
        dataBase64 = base64.b64encode(dataJson)
        print '[DATABASE64]' + dataBase64
        httpClient = httplib.HTTPConnection(self.url)
        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
        param = {'code': dataBase64}
        data = urllib.urlencode(param).replace('%3D', '=')
        print '[DATA]' + data
        httpClient.request('POST', self.path, dataBase64, headers)
        response = httpClient.getresponse()
        print '[RESPONSE]' + response.read()
        #dataArray = json.loads(response.read(), encoding="utf-8")
        #print dataArray

    def getImpl(self):
        pass
        



#r = RequestUtil('www.baidu.com', '/s', {'wd': 'aaa'})
#r = RequestUtil('182.92.164.224', '/api/duobaochuan/registerapi2.php', {'username': 'lczgywzyy', 'password': '1q2w3e4r', 'email': 'xyq547133@163.com'})
r = RequestUtil('182.92.164.224', '/api/duobaochuan/registerapi3.php', {'username': 'lczgywzyy', 'password': '1q2w3e4r', 'email': 'xyq547133@163.com'})
r.postImpl()
